package com.zxct.wyserver.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.zxct.wyserver.R;

/**
 * Created by lanyu on 2018/1/30.
 */

public class DialogUtils {

    public static DialogUtils getInstance() {
        return new DialogUtils();
    }

    /**
     * 带有确认取消按钮的自定义dialog
     *
     * @param context 上下文
     * @param message 显示的信息
     */
    public static void showDialog(final Activity context, String message, String confirm, String cancel) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog alertDialog = builder.create();
//        final Dialog alertDialog = new Dialog(context, R.style.my_share_dialog);

        //设置点击屏幕取消事件
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //隐藏底部虚拟按钮
                //BarUtils.hideVirtualKey(context);
                dialog.dismiss();
            }
        });
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        View view = View.inflate(context, R.layout.alert_dialog, null);
        TextView tvMsg = (TextView) view.findViewById(R.id.tv_dialog_massage);
        TextView tvCancel = (TextView) view.findViewById(R.id.btn_cancel);
        TextView tvConfirm = (TextView) view.findViewById(R.id.btn_ok);

        tvMsg.setText(message);
        tvCancel.setText(cancel);
        tvConfirm.setText(confirm);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //隐藏底部虚拟按钮
                //BarUtils.hideVirtualKey(context);
                onButtonClickListener.onNegativeButtonClick(alertDialog);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //隐藏底部虚拟按钮
                //BarUtils.hideVirtualKey(context);
                onButtonClickListener.onPositiveButtonClick(alertDialog);
            }
        });
        //BarUtils.hideBottomBar(alertDialog);
        window.setContentView(view);
    }

    private static OnButtonClickListener onButtonClickListener;

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    //按钮点击回调接口
    public interface OnButtonClickListener {
        /**
         * 确定按钮点击回调方法
         *
         * @param dialog 当前 AlertDialog，传入它是为了在调用的地方对 dialog 做操作，比如 dismiss()
         *               也可以在该工具类中直接  dismiss() 掉，就不用将 AlertDialog 对象传出去了
         */
        void onPositiveButtonClick(AlertDialog dialog);

        /**
         * 取消按钮点击回调方法
         *
         * @param dialog 当前AlertDialog
         */
        void onNegativeButtonClick(AlertDialog dialog);
    }


}
